/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.model.Brand;
import com.yami.shop.bean.model.CategoryBook;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.CategoryBookService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yami.shop.common.response.ServerResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.util.PageParam;
import cn.hutool.core.util.StrUtil;

import java.util.Date;
import java.util.List;
import java.util.Objects;



/**
 * 分类管理
 * @author lgh
 *
 */
@RestController
@RequestMapping("/shop/categoryBook")
public class CategoryBookController {

	@Autowired
	private CategoryBookService categoryBookService;

	/**
	 * 获取菜单页面的表
	 * @return
	 */
	// @GetMapping("/page")
	// @PreAuthorize("@pms.hasPermission('shop:categoryBook:page')")
	// public ServerResponseEntity<List<CategoryBook>> getBookPage(){
	// 	List<CategoryBook> categoryBookMenuList = categoryBookService.tableCategoryBook(SecurityUtils.getSysUser().getShopId());
	// 	return ServerResponseEntity.success(categoryBookMenuList);
	// }
    /**
     * 分页获取
     */
    // @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('shop:categoryBook:page')")
    // public ServerResponseEntity<IPage<CategoryBook>> page(CategoryBook categoryBook,PageParam<CategoryBook> page) {
    //     IPage<CategoryBook> categoryBooks = CategoryBookService.page(page,
    //             new LambdaQueryWrapper<CategoryBook>()
    //                     .like(StrUtil.isNotBlank(categoryBook.getTitle()), CategoryBook::getTitle, categoryBook.getTitle()).orderByAsc(CategoryBook::getFirstChar));
    //     return ServerResponseEntity.success(categoryBooks);
    // }
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('shop:categoryBook:page')")
    public ServerResponseEntity<IPage<CategoryBook>> page(CategoryBook categoryBook, PageParam<CategoryBook> page) {
        IPage<CategoryBook> categoryBooks = categoryBookService.page(page,
                new LambdaQueryWrapper<CategoryBook>()
                        .like(StrUtil.isNotBlank(categoryBook.getTitle()), CategoryBook::getTitle, categoryBook.getTitle())
                        .orderByAsc(CategoryBook::getSortOrder));  // 改为使用实际存在的字段排序
        return ServerResponseEntity.success(categoryBooks);
    }

	/**
	 * 获取分类信息
	 */
	@GetMapping("/info/{categoryBookId}")
	public ServerResponseEntity<CategoryBook> info(@PathVariable("categoryBookId") Long categoryBookId){
		CategoryBook categoryBook = categoryBookService.getById(categoryBookId);
		return ServerResponseEntity.success(categoryBook);
	}



	/**
	 * 保存分类
	 */
	@SysLog("保存分类")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('shop:categoryBook:save')")
	public ServerResponseEntity<Void> save(@RequestBody CategoryBook categoryBook){
		categoryBookService.saveCategoryBook(categoryBook);
		return ServerResponseEntity.success();
	}

	/**
	 * 更新分类
	 */
	@SysLog("更新分类")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('shop:categoryBook:update')")
	public ServerResponseEntity<String> update(@RequestBody CategoryBook categoryBook){
		categoryBookService.updateCategoryBook(categoryBook);
		return ServerResponseEntity.success();
	}

	/**
	 * 删除分类
	 */
	@SysLog("删除分类")
	@DeleteMapping("/{categoryBookId}")
	@PreAuthorize("@pms.hasPermission('shop:categoryBook:delete')")
	public ServerResponseEntity<String> delete(@PathVariable("categoryBookId") Long categoryBookId){
		if (categoryBookService.count(new LambdaQueryWrapper<CategoryBook>().eq(CategoryBook::getFid,categoryBookId)) >0) {
			return ServerResponseEntity.showFailMsg("请删除子分类，再删除该分类");
		}
		categoryBookService.deleteCategoryBook(categoryBookId);
		return ServerResponseEntity.success();
	}

	/**
	 * 所有的
	 */
	@GetMapping("/listCategoryBook")
	public ServerResponseEntity<List<CategoryBook>> listCategoryBook(){

		return ServerResponseEntity.success(categoryBookService.list(new LambdaQueryWrapper<CategoryBook>()
														.orderByAsc(CategoryBook::getId)));
	}

	/**
	 * 所有的产品分类
	 */
	/*@GetMapping("/listProdCategoryBook")
	public ServerResponseEntity<List<CategoryBook>> listProdCategoryBook(){
    	List<CategoryBook> categories = categoryBookService.treeSelect(SecurityUtils.getSysUser().getShopId(),2);
		return ServerResponseEntity.success(categories);
	}*/
}
